from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.market_data import MarketData
import glob
import jieba
import re
import threading
import hashlib
import json
from database.db_business import DbBusiness
from common.entity_data import EntityData

'''
NBA球员信息
'''

class EntitySetNbaQiuyuan:
    def __init__(self):
        self.business = DbBusiness()
        self.entity = EntityData()
        pass

    def get_data(self):
        url = "http://slamdunk.sports.sina.com.cn/api?p=radar&callback=jQuery1113014198299609674248_1653553951953&s=team&a=rosters&limit=24&_=1653553951954"
        proxy = self.business.query_proxy()
        json_data = json.loads(MyHttp.http_json_data_v2(url, proxy).decode().replace("try{jQuery1113014198299609674248_1653553951953(","").replace(");}catch(e){};",""))

        for d in json_data["result"]["data"]["teams"]:
            tid = d["team"]["tid"]
            qiudui_url = "https://slamdunk.sports.sina.com.cn/api?p=radar&callback=jQuery11130237090964142175_1653555048150&p=radar&s=team&a=roster&tid=" + tid + "&season=2021&_=1653555048151"
            qiuyuan_json_data = json.loads(MyHttp.http_json_data_v2(qiudui_url, proxy).decode().replace("try{jQuery11130237090964142175_1653555048150(","").replace(");}catch(e){};",""))
            name_list = ""
            qiudui_name = qiuyuan_json_data["result"]["data"]["team"]["name"]
            for qiuyuan in qiuyuan_json_data["result"]["data"]["roster"]:
                name_list += "|" + qiuyuan["last_name_cn"]
            print(qiudui_name + name_list)
            self.entity.add_entity_data("NBA" + qiudui_name + "球队成员", "", 200, 2000003, "NBA" + qiudui_name, "", name_list)
            self.entity.update_entity_data("NBA" + qiudui_name, "球队成员", 200, 2000003, "NBA" + qiudui_name, "", name_list)


def entity_set_nba_qiuyuan_api():
    d = EntitySetNbaQiuyuan()
    d.get_data()

if __name__ == '__main__':
    entity_set_nba_qiuyuan_api()
